﻿@{
    Layout = "~/Views/Shared/MasterPage.cshtml";
    ViewBag.Title = "JAVA代码生成器";
}
<style type="text/css">
    .eleHide {
        display: none;
    }

    .divContent {
        box-shadow: 0 0 10px grey;
        padding: 20px;
    }
</style>
<div id="pageMSSQL" class="divPage">
    <div class="divTitleText"></div>
    <div class="divCondition">
        <div>
            <button id="btnConnect" class="jk-button mr10">连接</button>
            <button id="btnGenerat" class="jk-button-disabled mr10" disabled="disabled">生成</button>
            <button id="btnExport" class="jk-button-disabled mr10" disabled="disabled">下载</button>
            <button id="btnExportAll" class="jk-button-disabled" style="width:110px" disabled="disabled">下载全部表</button>
        </div>
        <table style="margin-top: 10px;width:100%">
            <tr>
                <td width="90px">数据库类型： </td>
                <td>
                    <input type="radio" name="dbType" id="cboSqlServer" checked="checked" value="SqlServer" />
                    <label for="cboSqlServer">Sql Server</label>
                    <input type="radio" name="dbType" id="cboMySql" value="MySql" />
                    <label for="cboMySql">MySql</label>
                    <input type="radio" name="dbType" id="cboPostgreSql" value="PostgreSql" />
                    <label for="cboPostgreSql">PostgreSql</label>
                </td>
            </tr>
            <tr>
                <td>连接字符串：</td>
                <td>
                    <input type="text" id="txtConnectionString" style="max-width: 686px;width: 100%" value="data source=xxxxxxxxxx;database=xxxxxxxxxx;uid=xxxxxxxxxx;pwd=xxxxxxxxxx;" />
                </td>
            </tr>
            <tr class="eleHide">
                <td>命名空间：</td>
                <td>
                    <input type="text" id="txtPackage" style="max-width: 686px;width: 100%" value="com.xxx.xxx.{0}" />
                </td>
            </tr>
            <tr class="eleHide">
                <td>表和视图：</td>
                <td>
                    <select id="sltTables" style="max-width:700px;width:100%"></select>
                </td>
            </tr>
            <tr class="eleHide">
                <td>类名：</td>
                <td>
                    <input type="text" id="txtClassName" class="" style="max-width: 686px;width: 100%" />
                </td>
            </tr>
            <tr class="eleHide">
                <td>类型：</td>
                <td>
                    <select id="sltDataType" style="max-width:700px;width:100%"></select>
                </td>
            </tr>
        </table>
    </div>
    <pre id="divCode" class="divContent" style="display: none">
    </pre>
    <form method="post" id="fmExport" style="display: none" action="/CodeMaker/DownloadFile">
        <input name="content" />
        <input name="fileName" />
        <input name="extension" value="java" />
    </form>
    <form method="post" id="fmExportAll" style="display: none" action="/CodeMaker/DownloadAllFile">
        <input name="Lang" />
        <input name="ClassName" />
        <input name="ConnectionString" />
        <input name="Package" />
        <input name="DbType" />
        <input name="Type" />
        <input name="extension" value="java" />
    </form>
</div>
<script type="text/javascript">

    $(function () {
        InitCodeTypeSlt();
    });

    function InitCodeTypeSlt() {
        $.getJSON("/CodeMaker/GetCodeTypeSlt?lang=Java&r=" + new Date().getTime(), function (data) {
            $("#sltDataType").html('');
            var html = '';
            for (var i = 0; i < data.length; i++) {
                html += "<option value='" + data[i] + "'>" + data[i].replaceAll("Ref", "") + "</option>";
            }
            $("#sltDataType").html(html);
        });

    }

    $("#btnExport").bind("click", function () {
        var content = $("#divCode").text();
        if (content.trim() == '') return;
        $("#fmExport").find("input[name=content]").val(encodeURIComponent(content));
        $("#fmExport").find("input[name=fileName]").val($("#txtClassName").val());
        $("#fmExport").submit();
    });

    $("#btnExportAll").bind("click", function () {
        $("#fmExportAll").find("input[name=ConnectionString]").val(encodeURIComponent($("#txtConnectionString").val()));
        $("#fmExportAll").find("input[name=Package]").val($("#txtPackage").val());
        $("#fmExportAll").find("input[name=DbType]").val($("input:radio[checked=checked]").val());
        $("#fmExportAll").find("input[name=Lang]").val("Java");
        $("#fmExportAll").find("input[name=Type]").val($("#sltDataType").val());
        $("#fmExportAll").find("input[name=ClassName]").val($("#txtClassName").val());
        $("#fmExportAll").submit();
    });

    document.body.oncopy = function () {
        setTimeout(function () {
            var text = clipboardData.getData("text");
            if (text) {
                text = text + "\r\n本篇文章来源于 codetool 原文链接：" + location.href;
                clipboardData.setData("text", text);
            }
        }, 100)
    }

    $("#btnGenerat").bind("click", function () {
        var connectionString = $("#txtConnectionString").val();
        var table = $("#sltTables").val();
        var pkg = $("#txtPackage").val();
        var className = $("#txtClassName").val();
        var type = $("#sltDataType").val();
        $.getJSON("/CodeMaker/GeneratCode", {
            lang: "Java",
            DbType: $("input:radio[checked=checked]").val(),
            ConnectionString: encodeURIComponent(connectionString),
            Table: table,
            Package: pkg,
            Type: type,
            ClassName: className,
            Random: new Date().getTime()
        }, function (result) {
            if (result != null && result.Message != null) {
                alert(result.Message);
            }
            else {
                $("#divCode").fadeIn(200);
                $("#divCode").text(result);
                $("#btnExport").addClass("jk-button").removeClass("jk-button-disabled").removeAttr("disabled", "");
                $("#btnExportAll").addClass("jk-button").removeClass("jk-button-disabled").removeAttr("disabled", "");
                if (type.indexOf('Xml') > 0) {
                    $("#fmExport,#fmExportAll").find("input[name=extension]").val('xml');
                }
                else {
                    $("#fmExport,#fmExportAll").find("input[name=extension]").val('java');
                }
            }
        });
    });

    $("input:radio").bind("change", function () {
        var connectionString = $("#txtConnectionString").val();
        var sqlserver = "data source=xxxxxxxxxx;database=xxxxxxxxxx;uid=xxxxxxxxxx;pwd=xxxxxxxxxx;";
        var mysql = "data source=xxxxxxxxxx;database=xxxxxxxxxx;uid=xxxxxxxxxx;pwd=xxxxxxxxxx;port=xxxx;";
        var postgresql = "server=xxxxxxxxxx;database=xxxxxxxxxx;uid=xxxxxxxxxx;pwd=xxxxxxxxxx;port=xxxx;";

        if (this.value == 'SqlServer') {
            if ($("#txtConnectionString").val() == postgresql || $("#txtConnectionString").val() == mysql || $("#txtConnectionString").val() == "") {
                $("#txtConnectionString").val(sqlserver);
            }
        } else if (this.value == 'PostgreSql') {
            if ($("#txtConnectionString").val() == sqlserver || $("#txtConnectionString").val() == mysql || $("#txtConnectionString").val() == "") {
                $("#txtConnectionString").val(postgresql);
            }
        } else if (this.value == 'MySql') {
            if ($("#txtConnectionString").val() == sqlserver || $("#txtConnectionString").val() == postgresql || $("#txtConnectionString").val() == "") {
                $("#txtConnectionString").val(mysql);
            }
        }
    });

    $("#divCode").bind("mouseover", function () {
        SelectText($(this));
    });

    function GetClassName(str) {
        if (str != null && str.length > 2 && str.substring(0, 2) == 't_') {
            str = str.substring(2);
        }
        return str.replaceUnderline().toUpperFirst();
    }

    $("#sltTables").bind("change", function () {
        $("#txtClassName").val(GetClassName($(this).find("option:selected").val()));
    });

    $("#btnConnect").bind("click", function () {
        var connectionString = $("#txtConnectionString").val();
        if (connectionString.trim() == "") {
            return;
        }
        $.ajax({
            async: true,
            type: "POST",
            url: "/CodeMaker/GetTables",
            cache: false,
            timeout: 60 * 60 * 1000,
            dataType: "json",
            data: {
                ConnectionString: encodeURIComponent(connectionString),
                Random: new Date(),
                DbType: $("input:radio[checked=checked]").val()
            },
            success: function (result) {
                if (result != null && result.Message != null) {
                    alert(result.Message);
                    $(".eleHide").hide();
                }
                else {
                    $(".eleHide").fadeIn(200);
                    $("#btnGenerat").addClass("jk-button").removeClass("jk-button-disabled").removeAttr("disabled", "");
                    $("#btnExportAll").addClass("jk-button").removeClass("jk-button-disabled").removeAttr("disabled", "");
                    var tables = $("#sltTables");
                    var html = "";
                    if (result.length > 0) {
                        for (var i = 0; i < result.length; i++) {
                            html = html + "<option value='" + result[i] + "'>" + result[i] + "</option>";
                        }
                        $("#txtClassName").val(GetClassName(result[0]));
                    }
                    tables.html(html);
                }
            }
        });
    });
</script>
